import { Config } from "config.js";
class Comm {
    constructor() {
            this.baseRestUrl = Config.baseUrl;
        }
        /* 请求数据接口封装
          params = {
             url:url,//接口地址
             data:{},//请求参数
             type:GET/POST,//请求类型，默认为POST
             sCallBack(value){},//成功回调
             eCallBack(value){}//失败回调
          }
         */
    request(params) {
            let that = this;
            let url = this.baseRestUrl + params.url;
            if (!params.type) {
                params.type = "POST";
            }
            if (!params.data) {
                params.data = {}
            }
            params.data.platform = "MINI";
            params.data.token = wx.getStorageSync('token');
            params.data.schoolId = params.data.schoolId || wx.getStorageSync('schoolId');
            wx.request({
                url: url,
                data: params.data,
                header: {
                    'content-type': 'application/json'
                },
                method: params.type,
                success: function(res) {
                    let code = res.statusCode.toString();
                    let startChar = code.charAt(0);
                    if (startChar == 2) {
                        if (res.data.flag === 1) {
                            params.sCallBack && params.sCallBack(res.data);
                        } else if (res.data.flag === -100) {
                            that._refresh(params);
                        } else {
                            params.eCallBack && params.eCallBack(res.data);
                        }
                    } else {
                        params.eCallBack && params.eCallBack(res.data);
                    }
                },
                fail: function(res) {
                    params.eCallBack && params.eCallBack(res.data);
                }
            })
        }
        //刷新token
    _refresh(params) {
        wx.login({
            success: res => {
                if (res.code) {
                    let props = {
                        url: "/gateway/api/login/wxOpen",
                        type: "GET",
                        data: {
                            code: res.code
                        },
                        sCallBack: res => {
                            wx.setStorageSync("token", res.data.Token);
                            wx.setStorageSync("uId", res.data.UserId);
                            this.request(params);
                        },
                        eCallBack: err => {
                            wx.showToast({
                                title: err.msg,
                            })
                        }
                    }
                    this.request(props);
                } else {
                    wx.showToast({
                        title: '登录失败',
                        icon: "none"
                    })
                }
            }
        })
    }
};

export { Comm };